Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

retry-axios

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

retry-axios

Retry HTTP requests with Axios.

  • 3.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
298K
decreased by-54.49%
Maintainers
1
Weekly downloads
 
Created

What is retry-axios?

retry-axios is a lightweight library that adds automatic retry functionality to axios requests. It allows you to specify retry conditions, delay strategies, and maximum retry attempts, making it easier to handle transient errors in HTTP requests.

What are retry-axios's main functionalities?

Automatic Retries

This feature allows you to automatically retry failed HTTP requests based on specified conditions. The code sample demonstrates how to configure retry-axios to retry a GET request up to 3 times with a delay of 100ms between attempts.

const axios = require('axios');
const rax = require('retry-axios');

const instance = axios.create();
instance.defaults.raxConfig = {
  instance,
  retry: 3,
  noResponseRetries: 2,
  retryDelay: 100,
  httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT'],
  statusCodesToRetry: [[100, 199], [429, 429], [500, 599]],
  onRetryAttempt: err => {
    const cfg = rax.getConfig(err);
    console.log(`Retry attempt #${cfg.currentRetryAttempt}`);
  }
};

rax.attach(instance);

instance.get('https://example.com')
  .then(response => console.log(response.data))
  .catch(error => console.error(error));

Custom Retry Logic

This feature allows you to define custom retry logic. The code sample shows how to retry a request only if the response status is 503 (Service Unavailable) and the retry attempt is less than 3.

const axios = require('axios');
const rax = require('retry-axios');

const instance = axios.create();
instance.defaults.raxConfig = {
  instance,
  retry: 3,
  retryDelay: 100,
  shouldRetry: (err) => {
    const cfg = rax.getConfig(err);
    return err.response.status === 503 && cfg.currentRetryAttempt < 3;
  }
};

rax.attach(instance);

instance.get('https://example.com')
  .then(response => console.log(response.data))
  .catch(error => console.error(error));

Exponential Backoff

This feature allows you to use exponential backoff for retry delays. The code sample demonstrates how to configure retry-axios to use exponential backoff with a base delay of 100ms.

const axios = require('axios');
const rax = require('retry-axios');

const instance = axios.create();
instance.defaults.raxConfig = {
  instance,
  retry: 3,
  retryDelay: 100,
  backoffType: 'exponential',
  onRetryAttempt: err => {
    const cfg = rax.getConfig(err);
    console.log(`Retry attempt #${cfg.currentRetryAttempt}`);
  }
};

rax.attach(instance);

instance.get('https://example.com')
  .then(response => console.log(response.data))
  .catch(error => console.error(error));

Other packages similar to retry-axios

Keywords

FAQs

Package last updated on 16 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc